<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Car_info;
use DB;
 

class CarController extends Controller
{

   /* //利用中间件检测用户的登录状态.
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[] ];*/
    
    public function getaddCar($cyh){
        $car_cyh = [$cyh];

        $cat = DB::table('cates')->where('parent_id',-1)->get();
        // dd($cat);
        return view('admin.addcarinfo',['cate'=>$cat,'car_cyh'=>$car_cyh]);
    }

    public function cheXi($cate_id){
        $chexi = DB::table('cates')->where('parent_id',$cate_id)->get();
        return $chexi;

    }

    public function editCar($cid){
       if(empty($_POST)){
        $car = DB::table('car_infos')->where('car_id',$cid)->first();
            return view('admin.editcar',['car'=>$car]); 
        }else{
           $row = [
           'car_address' => $_POST['car_address'],
           "car_mide" => $_POST["car_mide"],
           'car_biansu' => $_POST['car_biansu'],
           'car_price' => $_POST['car_price'],
           'car_reprice' => $_POST['car_reprice'],
           'car_licheng' => $_POST['car_licheng'],
           'car_pailiang' => $_POST['car_pailiang'],
           'car_pfbz' => $_POST['car_pfbz'],
           'car_sp' => $_POST['car_sp'],
           'car_age' => $_POST['car_age'],
           'status' => $_POST['car_status']

           ];
         return  DB::table('car_infos')->where('car_id',$cid)->update($row)?redirect('carlist'):'false';
     }
    }

    public function delcar($cid){
       return DB::table('car_infos')->where('car_id',$cid)->delete()?redirect('carlist'):"失败";
           
    }

    public function postaddCar(request $req){
        $row = [];
        $city = $req->s_province.$req->s_city.$req->s_county;
        $row['car_address'] = $city;
        $row['car_brand'] = $req->car_brand;
        $row['cate_id'] = $req->car_chexi;
        $row['car_type'] = $req->car_type;
        $row['car_name'] = $req->car_name;
        $row['car_cyh'] = $req->car_cyh;
        $row['car_mide'] = $req->car_mide;
        $row['car_biansu'] = $req->car_biansu;
        $row['car_price'] = $req->car_price;
        $row['car_reprice'] = $req->car_reprice;
        $row['car_licheng'] = $req->car_licheng;
        $row['car_pailiang'] = $req->car_pai;
        $row['car_pfbz'] = $req->car_biaozhun;
        $row['car_sp'] = $req->car_sp;
        $row['status'] = $req->car_status;
        $row['car_age'] = $req -> car_age;

        $row['car_color'] = $req ->car_color;
        $row['pricefanwei'] = $req->pricefanwei;
       
        //开始文件上传
        $path = public_path().'/uploads';
        $fileName = mt_rand(1000,9999).'.jpg';
        $req->file('car_img')->move($path,$fileName);
        //将原图片路径写入数据库
        $row['car_img'] = '/uploads' . '/' . $fileName;
         $row['car_thumb'] = '/uploads' . '/' . $fileName;


        $aa = DB::table('car_infos')->insert($row);
        if($aa){
           // var_dump($row['car_img']);
           return redirect("carlist");
        }

    }

  
    public function carList(){
        $car = DB::table('car_infos')->select('car_id','car_address','car_name','car_cyh','car_price','car_reprice','car_img')->paginate(10);
        return view('admin.carlist',['car'=>$car]);
    }
    public function maisj($msj){
        //$row['s_mobile']=$msj;
        // return $msj;
        DB::table('seller_infos')->insert(['s_mobile'=>$msj]);
        $aa = 1;
        echo $aa ;
    }
    public function maiChe(){
        return view('home.maiche');
    }
    //周金阳所写carsshow
    public function carsshow($car_id){
        $car = DB::table('car_infos')->where('car_id',$car_id)->first();
        // dd($car);
        return view('home.carsshow',['car'=>$car]);
    }

}
